package com.max.xihu.concurrent.day01;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: Max
 * @Date: 2020-07-11 09:05
 * @Description:
 */
public class CasTest {

//    public static volatile int race = 0;

    public static AtomicInteger race = new AtomicInteger(0);

    private static final int THREAD_HOLE = 20;

    private static CountDownLatch latch = new CountDownLatch(THREAD_HOLE);

    public static void increase(){
        race.getAndIncrement();
    }

    public static void main(String[] args) throws InterruptedException{
        long start = System.currentTimeMillis();
        Thread[] thread = new Thread[THREAD_HOLE];
        for (int i = 0; i < THREAD_HOLE; i++){
            thread[i] = new Thread(() -> {
                for (int i1 = 0; i1 < 10000; i1++){
                    increase();
                }
                latch.countDown();
            });
            thread[i].start();
        }
        latch.await();
        System.out.println(race);
        System.out.println("耗时："+(System.currentTimeMillis()-start)+"ms");
    }
}
